//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#ifndef __ELASTOS__DRIVER_H__
#define __ELASTOS__DRIVER_H__

#define DEVICENO_NONE           ((uint_t)(-1))
#define DRIVER_SERVICE_PREFIX   L"device:"

const uint_t  c_uMaxLengthOfDeviceName          = 31;
const uint_t  c_uMaxLengthOfUIntString          = 11;
const uint_t  c_uMaxLengthOfDriverServiceName   =
        lengthof(DRIVER_SERVICE_PREFIX) - 1 + c_uMaxLengthOfDeviceName
            + c_uMaxLengthOfUIntString;

typedef struct DeviceConfig
{
    wchar_t     wszDeviceName[c_uMaxLengthOfDeviceName + 1];
    uint_t      uDeviceNo;
    void        *pvParameter;
} DeviceConfig;

typedef IDeviceDriver *(CDECL *createdriverroutine_t)(
        uint_t uDeviceNo, void *pvParameter);

typedef struct DriverConfig
{
    wchar_t                 wszDeviceName[c_uMaxLengthOfDeviceName + 1];
    createdriverroutine_t   routine;
} DriverConfig;

//
//  Drivers configure for Kernel
//
typedef struct DzConfig
{
    DeviceConfig    *pDeviceConfigs;
    uint_t          uNumberOfDeviceConfigs;

    DriverConfig    *pDriverConfigs;
    uint_t          uNumberOfDriverConfigs;
} DzConfig;

EXTERN DzConfig dzcfg;

//
//  Driver
//
class CDeviceDriver : public IDeviceDriver
{
public:
    CARAPI_(PInterface) Probe(
         /* [in]  */ REIID riid);

    CARAPI_(UInt32) AddRef(void);

    CARAPI_(UInt32) Release(void);

    CARAPI Aggregate(
        /* [in] */ AggrType aggrType,
        /* [in] */ PInterface pObject);

    CARAPI GetDomain(
         /* [out] */ PInterface *ppObj);

    CDeviceDriver()            {}
    virtual ~CDeviceDriver()   {}

    virtual void Dispose() = 0;

protected:
    Interlocked     m_cRef;
};

EXTERN ECode CDECL DzRegisterDevice(
        const wchar_t *wszDeviceName, uint_t uDeviceNo, void *pvParameter);

EXTERN ECode CDECL DzUnregisterDevice(
        const wchar_t *wszDeviceName, uint_t uDeviceNo);

EXTERN void BuildDriverServiceName(
        wchar_t wszDriverServiceName[c_uMaxLengthOfDriverServiceName + 1],
        const wchar_t *wszDeviceName, uint_t uDeviceNo);

#ifdef __cplusplus
extern "C" {
#endif

#if defined(_arm)
///////////////////////////////////////////////////////////
#define EVENT_MAX       128
#define EVENT_KEYPAD    1
#define EVENT_TOUCH     2

#define EVENT_KEYSOUND          3   //key sound
#define EVENT_TOUCHSOUND        4   //touch-panel sound

#define EVENT_RAWKPD    5

#define EVENT_MiniSD    10
#define EVENT_COMMON    12

#define EVENT_TIMER0          36   //channel 0,10 ms
#define EVENT_TIMER1          37   //channel 1,s
#define EVENT_TIMER2          38   //channel 2,min
#define EVENT_TIMER3          39   //channel 3,h
#define EVENT_TIMER           40   //user timer start point
#define EVENT_POWER           80   //power manager queue
#define EVENT_USER            81   //user queue
#define EVENT_IDLE            82   //cpu idle mode
#define EVENT_INPUT           83   //input event
#define EVENT_SLEEP           84   //system sleep event
#define EVENT_LOCK_SCREEN     85   //system lock event
#define EVENT_IDLE_WAKEUP     86   //system idle-wakeup event
#define EVENT_WAKE            87   //system wake event
#define EVENT_BATTERY         88   // battery event, Ex: charger insert/remove...
#define EVENT_AUTO_SLEEP      89   //system auto lock event

#define EVENT_APP_STATE       90   //application state switch event (start/pause/stop)
#define EVENT_BUS_POWER       91   //bus device power event (power On/Off)
#define EVENT_REBOOT          92   //system reboot
#define EVENT_DEBUG           99
#define EVENT_REG_DEVICE      100  //register device

typedef struct
{
    int eType;//Event type
    int Size;//Queue element size
    int Len;//Queue array length
}QueueInfo;

typedef void (*TimerCallBackProc)(void * pvArg);
typedef struct
{
    TimerCallBackProc   pCallbackProc;
    void                *pvArg;
    int                 eType;          //QueueNo
    UInt32              Resolution;
    UInt32              Interval;
} TimerInfo;

///////////////////////////////////////////////////////////
extern IDeviceDriver * g_pEventQueues;

int AddNotify(int EventType, const void *pNotiProc);
void NotifyByIsr(int eType, int nCode, void *pParam);
void InputNotifyByIsr(int eType, int nCode, void *pParam);

//==========================================================================
// Chat Debug module functions
//==========================================================================
int __cdecl ChatInit();
int __cdecl _ChatInit(int Port, unsigned long Baud);
int __cdecl cprintf(const char *format, ...);
void __cdecl cputs(const char *s);
void __cdecl chat();
#ifndef Chat
#define Chat() {cprintf("PASS: %S[%d]\n", __FILE__, __LINE__);chat();}
#endif
#ifndef CDBGPRN
#define CDBGPRN(x) cprintf(#x"= %x\n", x)
#endif
#endif

#ifdef __cplusplus
}
#endif

#endif //__ELASTOS__DRIVER_H__
